package com.jf.yytgd.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf.yytgd.entity.dto.like.LikeDTO;
import com.jf.yytgd.entity.dto.like.LikeExistDTO;
import com.jf.yytgd.entity.dto.scienceArticle.ScienceArticleBarDTO;
import com.jf.yytgd.entity.dto.scienceArticle.ScienceArticleFullDTO;
import com.jf.yytgd.service.ScienceArticleService;
import com.jf.yytgd.utils.Msg;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 * Description: 科普文章控制层
 * User:22954
 * Date:2025-01-16
 * Time:17:02
 */
@RequestMapping("/article")
@RestController
public class ScienceArticleController {
    @Resource
    private ScienceArticleService articleService;

    @GetMapping("/list")
    public Msg listBy(@RequestParam(value = "page_no", defaultValue = "1") Integer pageNo,
                      @RequestParam(value = "page_size", defaultValue = "15") Integer pageSize,
                      @RequestParam(value = "search", required = false) String title,
                      @RequestParam(value = "order", required = false) String order) {
        Page<ScienceArticleBarDTO> page = articleService.pageBar(pageNo, pageSize, title, order);
        Msg msg = Msg.success();
        msg.addData("articles", page);
        return msg;
    }

    @GetMapping()
    public Msg browse(@RequestParam("id") Integer id) {
        ScienceArticleFullDTO article = articleService.browse(id);
        Msg msg = Msg.success();
        msg.addData("article", article);
        return msg;
    }

    @PutMapping("/like")
    public Msg like(@RequestBody @Validated LikeDTO like) {
        articleService.like(like);
        return Msg.success();
    }

    @PostMapping("/like/exist")
    public Msg isLike(@RequestBody @Validated LikeExistDTO like) {
        //like.setRelatedType("ScienceArticle");
        boolean isLike = articleService.isLike(like);
        Msg success = Msg.success();
        success.addData("isLike", isLike);
        return success;
    }
}
